// eslint-disable-next-line @typescript-eslint/naming-convention
declare const GM_cookie: {
    list: (
        details: { name: string },
        callback: (
            cookies: Array<{
                value: string
            }>,
            err?: string,
        ) => void,
    ) => void
}

async function cookies(name: string): Promise<Array<{ value: string }>> {
    return await new Promise((resolve, reject) => {
        GM_cookie.list({ name }, (cookies, err) => {
            if (typeof err !== 'undefined' || cookies.length <= 0) {
                reject(new Error('empty'))
            } else {
                resolve(cookies)
            }
        })
    })
}

let currentUser = 0

async function initCurrentUser(): Promise<void> {
    try {
        currentUser = parseInt((await cookies('_uid'))[0].value)
    } catch (err) {}
}

export { initCurrentUser, currentUser }
